﻿// ===================================
// Author : Micro.SanVey
// Eamil  : sanvey.china@gmail.com
// ===================================

namespace U.Help.Common
{
    #region 引用
    using System;
    #endregion

    /// <summary>
    /// 常用转换类
    /// </summary>
    public static class ConvertEx
    {
        /// <summary>
        /// 将字符串进行URL编码
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns>URL编码过的字符串</returns>
        public static string UrlEncode(this string value)
        {
            // [DC] This is more correct than HttpUtility; it escapes spaces as %20, not +
            return Uri.EscapeDataString(value);
        }

        /// <summary>
        /// 将字符串进行URL解码
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns>URL解码过的字符串</returns>
        public static string UrlDecode(this string value)
        {
            return Uri.UnescapeDataString(value);
        }

        /// <summary>
        /// 将Byte数组转换为Base64编码字符串
        /// </summary>
        /// <param name="input">数组</param>
        /// <returns>Base64字符串</returns>
        public static string ToBase64String(this byte[] input)
        {
            return Convert.ToBase64String(input);
        }

        /// <summary>
        /// 将Base64编码字符串转换字符串
        /// </summary>
        /// <param name="input">字符串</param>
        /// <returns>Byte数组</returns>
        public static byte[] FromBase64String(this string input)
        {
            return Convert.FromBase64String(input);
        }
    }
}
